package com.yhm.myfootbollshow.demos.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhm.myfootbollshow.demos.entity.dto.PageBase;
import com.yhm.myfootbollshow.demos.entity.po.Merchant;
import com.yhm.myfootbollshow.demos.entity.po.User;
import com.yhm.myfootbollshow.demos.service.MerchantService;
import com.yhm.myfootbollshow.demos.mapper.MerchantMapper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
* @author lbw
* @description 针对表【merchant(商家表)】的数据库操作Service实现
* @createDate 2025-04-13 12:11:45
*/
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant>
    implements MerchantService{

    @Override
    public Boolean addMerchan(Merchant merchant) {
        System.out.println(merchant.toString());
        // 判断merchant是否为空
        if (merchant == null) {
            return false;
        }
        // 如果密码不为空，则调用加密方式加密密码并初始化信息
        if (merchant.getPassword() != null) {
            merchant.setPassword(new BCryptPasswordEncoder().encode(merchant.getPassword()));
            merchant.initMerchant();
            return save(merchant);
        }
        return false;
    }

    @Override
    public IPage<Merchant> list(PageBase page) {
        // 判断page是否为空
        if (page == null) {
            return null;
        }
        Page<Merchant> userPage = new Page<>();
        userPage.setCurrent(page.getPageNum());
        userPage.setSize(page.getPageSize());
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Merchant.class,info -> !info.getProperty().equals("password"))
                .like(page.getKeyword() != null,Merchant::getMerchantName,page.getKeyword())
                .eq(page.getStatus() != null&& !page.getStatus().isEmpty(),Merchant::getStatus,page.getStatus());
        return userPage.setRecords(baseMapper.selectList(userPage,wrapper));
    }

    @Override
    public Boolean banMerchant(Long merchantId, String username, Integer status) {
        System.out.println(merchantId+" "+username+" "+status);
        // 判断参数是否为空
        if (merchantId != null && username != null && status != null) {
            // 根据id查询商家
            Merchant merchant = baseMapper.selectById(merchantId);
            // 判断商家是否存在
            if (merchant != null) {
                // 修改商家状态
                merchant.setStatus(status);
                // 更新商家信息
                baseMapper.updateById(merchant);
                return true;
            }
        }

        return false;
    }
}




